package com.fmsz.xmly.fragments.findfgts;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.fmsz.xmly.R;
import com.fmsz.xmly.callbacks.FindTopListHttpService;
import com.fmsz.xmly.entitys.toplist.FindTopListEntity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class FindTopListFragment extends Fragment implements Callback<FindTopListEntity> {


    private View view;
    private RecyclerView recyclerView;
    private final String BASE_URL = "http://mobile.ximalaya.com/";
    private static final String TAG = "FindTopListFragment";

    public FindTopListFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (view==null) {
            view = inflater.inflate(R.layout.fragment_find__top_list_, container, false);
            initView(view);
            initData();
        }
        return view;
    }

    //初始化UI
    private void initView(View view) {
        recyclerView = ((RecyclerView) view.findViewById(R.id.recycle_find_top_list));
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

    }

    //初始化数据
    private void initData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FindTopListHttpService service = retrofit.create(FindTopListHttpService.class);
        service.getDatas().enqueue(this);
    }

    //数据下载结果的回调接口
    @Override
    public void onResponse(Call<FindTopListEntity> call, Response<FindTopListEntity> response) {
//        Log.i(TAG, "onResponse: "+(response.body().getFocusImages().getList().get(0).getPic()));
    }

    @Override
    public void onFailure(Call<FindTopListEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
